package com.cal.utms.model.maintenance;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Locale;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.cal.utms.dao.maintenance.HolidayDaoImpl;
import com.cal.utms.dto.maintenance.HolidayCalendarDto;
import com.cal.utms.entity.HolidayCalendar;

public class HolidayModel {

	HolidayDaoImpl daoImpl = new HolidayDaoImpl();

	public void saveHoliday(HolidayCalendarDto calendarDto, String holidayList) {
		List<HolidayCalendar> calendars = new ArrayList<HolidayCalendar>();
		if (holidayList != null) {
			holidayList = "{ \"holidayList\" : ".concat(holidayList) + " }";
			try {
				JSONObject jsonObject = new JSONObject(holidayList);
				JSONArray jArray = jsonObject.getJSONArray("holidayList");
				for (int i = 0; i < jArray.length(); i++) {
					JSONObject jObj = jArray.getJSONObject(i);
					HolidayCalendar calendar = new HolidayCalendar();
					String date = jObj.getString("date");
					Date holiday = null;
					try {
						holiday = new SimpleDateFormat("dd/MM/yyyy", Locale.ENGLISH).parse(date);
						calendar.setDate(holiday);
						calendar.setDescription(jObj.getString("description"));
						calendar.setBank(jObj.getBoolean("bank"));
						calendar.setPublic_(jObj.getBoolean("public_"));
						calendar.setMercantile(jObj.getBoolean("mercantile"));

					} catch (ParseException e) {
						e.printStackTrace();
					}
					calendars.add(calendar);
				}
			} catch (JSONException e) {
				e.printStackTrace();
			}

			if (calendars.size() > 0) {
				short result = daoImpl.saveHoliday(calendars);
				if (result == 1) {
					calendarDto.setSuccess(true);
				} else {
					calendarDto.setSuccess(false);
				}
			}
		}
	}

	public List<HolidayCalendarDto> getHolidays() {
		List<HolidayCalendar> calendars = daoImpl.getHolidays();
		List<HolidayCalendarDto> calendarDtos = new ArrayList<HolidayCalendarDto>();
		if (calendars != null) {
			for (HolidayCalendar holiday : calendars) {
				String date = new SimpleDateFormat("dd/MM/yyyy", Locale.ENGLISH).format(holiday.getDate());
				HolidayCalendarDto calendarDto = new HolidayCalendarDto(holiday.getBank(), date, holiday.getDescription(), holiday.getMercantile(), holiday.getPublic_());
				calendarDtos.add(calendarDto);
			}
		}
		return calendarDtos;
	}

	public List<HolidayCalendarDto> getBankHolidays() {
		List<HolidayCalendar> calendars = daoImpl.getBankHolidays();
		List<HolidayCalendarDto> calendarDtos = new ArrayList<HolidayCalendarDto>();
		if (calendars != null) {
			for (HolidayCalendar holiday : calendars) {
				String date = new SimpleDateFormat("dd/MM/yyyy", Locale.ENGLISH).format(holiday.getDate());
				HolidayCalendarDto calendarDto = new HolidayCalendarDto(holiday.getBank(), date, holiday.getDescription(), holiday.getMercantile(), holiday.getPublic_());
				calendarDtos.add(calendarDto);
			}
		}
		return calendarDtos;
	}

	public List<HolidayCalendarDto> getHolidaysByMonthYear(int year, int month) {
		List<HolidayCalendar> calendars = daoImpl.getHolidayByYearMonth(year, month);
		List<HolidayCalendarDto> calendarDtos = new ArrayList<HolidayCalendarDto>();
		if (calendars != null) {
			for (HolidayCalendar holiday : calendars) {
				String date = new SimpleDateFormat("dd/MM/yyyy", Locale.ENGLISH).format(holiday.getDate());
				HolidayCalendarDto calendarDto = new HolidayCalendarDto(holiday.getBank(), date, holiday.getDescription(), holiday.getMercantile(), holiday.getPublic_());
				calendarDtos.add(calendarDto);
			}
		}
		return calendarDtos;
	}
}
